<template>
  <div>
    <BasicTable @register="registerTable" ref="tableRef">
      <template #toolbar v-if="!teamGroupsSelete">
        <a-button type="primary" @click="handleCreate"> 发起资金预算 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '详情',
              icon: 'clarity:note-edit-line',
              onClick: handleDetail.bind(null, record),
              ifShow: !teamGroupsSelete,
            },
            {
              label: '进度',
              icon: 'clarity:note-edit-line',
              onClick: handleProcess.bind(null, record),
              ifShow: !teamGroupsSelete,
            },
          ]"
        />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" :type="type" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, toRefs, unref, ref } from 'vue';
  import { BasicTable, useTable, TableAction, TableActionType } from '/@/components/Table';
  import { getBudgetPage, addBudget, budgetDelete, getBudget } from '/@/api/backs/workFlow/budget';
  import { useDrawer } from '/@/components/Drawer';
  import Drawer from './components/Drawer.vue';
  import { columns, searchFormSchema } from './components/tableData';
  import { message } from 'ant-design-vue';
  import { Recoverable } from 'repl';
  import { useGo } from '/@/hooks/web/usePage';
  export default defineComponent({
    name: 'Budget',
    components: { BasicTable, Drawer, TableAction },
    props: {
      teamGroupsSelete: {
        type: Boolean,
        default: false,
      },
    },
    setup(props) {
      const go = useGo();
      const tableRef = ref<Nullable<TableActionType>>(null);
      const getTableAction = () => {
        const tableAction = unref(tableRef);
        if (!tableAction) {
          throw new Error('tableAction is null');
        }
        return tableAction;
      };
      const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
      const [registerTable, { reload, setSelectedRowKeys }] = useTable({
        title: '资金预算列表',
        api: getBudgetPage,
        columns,
        formConfig: {
          labelWidth: 100,
          schemas: searchFormSchema,
        },
        useSearchForm: !props.teamGroupsSelete,
        showTableSetting: !props.teamGroupsSelete,
        rowSelection: props.teamGroupsSelete,
        showIndexColumn: false,
        actionColumn: {
          title: '操作',
          dataIndex: 'action',
          width: '240px',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      const getSelectRowList = () => {
        return getTableAction().getSelectRows();
      };
      const setSelectedRowKeyList = (arr) => {
        setSelectedRowKeys(arr);
      };
      const data: any = reactive({
        type: 'add',
      });
      //新增
      const handleAddSuccess = async (formData) => {
        let params = { ...formData };
        try {
          await addBudget(params);
          closeDrawer();
          reload();
        } catch (error) {}
      };
      function handleCreate() {
        go('/workFlowManage/operationFlow/budgetCreate');
      }

      function handleDetail(record: Recordable) {
        go('/workFlowManage/operationFlow/budgetDetail/' + record.id);
      }

      //删除
      const handleDelete = async (record: Recordable) => {
        try {
          await budgetDelete({ id: record.id });
          message.success('删除成功！');
          reload();
        } catch (error) {}
      };
      const handleProcess = async (record: Recoverable) => {
        go('/workFlowManage/task/detail/' + record?.processInstanceId);
      };
      function handleSuccess(formData) {
        switch (data.type) {
          case 'add':
            handleAddSuccess(formData);
            break;
        }
      }

      return {
        ...toRefs(data),
        tableRef,
        registerTable,
        registerDrawer,
        handleCreate,
        handleDetail,
        handleDelete,
        handleSuccess,
        getSelectRowList,
        setSelectedRowKeyList,
        handleProcess,
      };
    },
  });
</script>
